#!/bin/bash

set -x
set -e

# 优化，尽可能编译优化拉满
# 使用 DPC/C++
# 修改源代码，适应 Intel 平台
CompileMode=optparamsdpcoptintel

ROOT_PATH=`cd $(dirname $0) && pwd`
SCRIPT_NAME=`basename $0`
SCRIPT=$ROOT_PATH/`basename $0`
cd ${ROOT_PATH}

module purge
module load compiler/2023.1.0
module load mpi/2021.11
# module load openmpi/4.1.6-mlnxofed-cuda12.3-gdrcopy-full
module load nvhpc-nompi/23.11
module load mkl/2023.1.0
module load cmake/3.22

mkdir -p ${ROOT_PATH}/softwares

# lapack
# cd ${ROOT_PATH}/lapack-3.11
# sed -i 's#CC = .*#CC = gcc#g' make.inc
# sed -i 's#FC = .*#FC = gfortran#g' make.inc
# make blaslib -j 16
# make cblaslib -j 16
# make lapacklib -j 16
# make lapackelib -j 16

# jemalloc
# cd jemalloc-5.3.0
# rm -rf build && mkdir build && cd build
# CC=icx CXX=icpx CFLAGS="-xHost -O3 -qopt-report -fp-model fast -qopt-report-file=${ROOT_PATH}/logs/optparamsdpcoptintel-jemallocc" CXXFLAGS="-xHost -O3 -qopt-report -fp-model fast -qopt-report-file=${ROOT_PATH}/logs/optparamsdpcoptintel-jemalloccxx" ../configure --enable-shared --prefix="${ROOT_PATH}/softwares/jemalloc/5.3.0/${CompileMode}"
# make -j 16 && make install

# parmetis
cd ${ROOT_PATH}/parmetis-4.0.3
## make config cc=mpiicc prefix=/es01/paratera/sce0588/zl/ASC/parmetis-4.0.3/parmetis-install
make config cc=mpiicx openmp=1 prefix=${ROOT_PATH}/softwares/parmetis/4.0.3/${CompileMode}
make -j 16
make install

# hypre
# cd ${ROOT_PATH}/hypre-2.28.0/src

# export LD_LIBRARY_PATH=${ROOT_PATH}/softwares/jemalloc/5.3.0/${CompileMode}/lib:$LD_LIBRARY_PATH

# CC=mpiicx CXX=mpiicpx FC=mpiifx FCFLAGS="-fPIC -xHost -O3 -qopt-report -qopt-report-file=${ROOT_PATH}/logs/optparamsdpcoptintel-hypref" CUCC="nvcc" LIBS="-ljemalloc" ./configure --prefix="${ROOT_PATH}/softwares/hypre/2.28.0/${CompileMode}" \
#     --enable-shared \
#     --with-MPI \
#     --with-openmp \
#     --with-extra-CFLAGS="-fPIC -xHost -O3 -qopt-report -qopt-report-file=${ROOT_PATH}/logs/optparamsdpcoptintel-hyprec" \
#     --with-extra-CXXFLAGS="-fPIC -xHost -O3 -qopt-report -qopt-report-file=${ROOT_PATH}/logs/optparamsdpcoptintel-hyprecxx" \
#     --with-extra-CUFLAGS="-ccbin icx -Xcompiler -fPIC,-xHost,-O3,-qopt-report,-qopt-report-file=${ROOT_PATH}/logs/optparamsdpcoptintel-hyprecu" \
#     --enable-onemklsparse --enable-onemklblas --enable-onemklrand \
#     --with-extra-ldpath="${ROOT_PATH}/softwares/jemalloc/5.3.0/${CompileMode}/lib" \

# ## compile
# make -j 16
# ## install
# make install

# petsc
# cd ${ROOT_PATH}/petsc-3.19.3

# export PETSC_DIR=${ROOT_PATH}/petsc-3.19.3
# export PETSC_ARCH=x86_64

# ./configure --with-cc=mpiicx \
#     --with-cxx=mpiicpx \
#     --with-fc=mpiifx \
#     --prefix="${ROOT_PATH}/softwares/petsc/3.19.3/${CompileMode}" \
#     --with-fortran-bindings=0 \
# 	--with-hypre-dir="${ROOT_PATH}/softwares/hypre/2.28.0/${CompileMode}" \
#     --with-debugging=0 \
#     --with-mpi=1 \
#     --with-openmp=1 \
#     --COPTFLAGS="-xHost -O3 -flto -ipo -qopt-report -fp-model fast -qopt-report-file=${ROOT_PATH}/logs/optparamsdpcoptintel-petscc" \
#     --CXXOPTFLAGS="-xHost -O3 -flto -ipo -qopt-report -fp-model fast -qopt-report-file=${ROOT_PATH}/logs/optparamsdpcoptintel-petsccxx" \
#     --FOPTFLAGS="-xHost -O3 -flto -ipo -qopt-report -fp-model fast -qopt-report-file=${ROOT_PATH}/logs/optparamsdpcoptintel-petscf" \
#     --LDFLAGS="-L${ROOT_PATH}/softwares/jemalloc/5.3.0/${CompileMode}/lib" \
#     --LIBS="-ljemalloc"

# make -j 20 PETSC_DIR=${PETSC_DIR} PETSC_ARCH=${PETSC_ARCH} all
# make all check

# petsc_solver
cd ${ROOT_PATH}/petsc_solver

export CC=mpiicx
export CXX=mpiicpx
export CPATH=${ROOT_PATH}/lapack-3.11/CBLAS/include:${ROOT_PATH}/lapack-3.11/LAPACKE/include:$CPATH

sed -i "s#set(PETSC_DIR .*)#set(PETSC_DIR ${ROOT_PATH}/petsc-3.19.3)#g" CMakeLists.txt
sed -i "s#set(PETSC_ARCH .*)#set(PETSC_ARCH ${PETSC_ARCH})#g" CMakeLists.txt

rm -rf build && mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS_RELEASE="-Wl,-L${ROOT_PATH}/softwares/jemalloc/5.3.0/${CompileMode}/lib -Wl,-ljemalloc -xHost -O3 -qopt-report -fp-model fast -qopt-report-file=${ROOT_PATH}/logs/optparamsdpcoptintel-petscsolvcxx"
make VERBOSE=1
mv libpetsc_solver.a ../lib/

# OpenCAEPoro
cd ${ROOT_PATH}/OpenCAEPoro

export CC=mpiicx
export CXX=mpiicpx

## users specific directory paths
export PARMETIS_DIR=${ROOT_PATH}/parmetis-4.0.3
export PARMETIS_BUILD_DIR=${PARMETIS_DIR}/build/Linux-x86_64
export METIS_DIR=${ROOT_PATH}/parmetis-4.0.3/metis
export METIS_BUILD_DIR=${PARMETIS_DIR}/build/Linux-x86_64
export PETSC_DIR=${ROOT_PATH}/petsc-3.19.3
export PETSC_ARCH=${PETSC_ARCH}
export PETSCSOLVER_DIR=${ROOT_PATH}/petsc_solver
export CPATH=${ROOT_PATH}/petsc-3.19.3/include/:$CPATH
export CPATH=${ROOT_PATH}/petsc-3.19.3/${PETSC_ARCH}/include/:${ROOT_PATH}/parmetis-4.0.3/metis/include:${ROOT_PATH}/parmetis-4.0.3/include:$CPATH
export CPATH=${ROOT_PATH}/lapack-3.11/CBLAS/include/:$CPATH

## install
rm -rf build; mkdir build; cd build;

echo "cmake -DUSE_PETSCSOLVER=ON -DUSE_PARMETIS=ON -DUSE_METIS=ON -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_BUILD_TYPE=Release -DUSE_OPENMP=ON .."
cmake -DUSE_PETSCSOLVER=ON -DUSE_PARMETIS=ON -DUSE_METIS=ON -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_BUILD_TYPE=Release -DUSE_OPENMP=ON -DCMAKE_CXX_FLAGS_RELEASE="-Wl,-L${ROOT_PATH}/softwares/jemalloc/5.3.0/${CompileMode}/lib -Wl,-ljemalloc -xHost -O3 -qopt-report -fp-model fast -qopt-report-file=${ROOT_PATH}/logs/optparamsdpcoptintel-opencaeporocxx" ..

make -j 32 && make install

# link OpenCAEPoro
mkdir -p ${ROOT_PATH}/softwares/OpenCAEPoro/${CompileMode}/lib
mkdir -p ${ROOT_PATH}/softwares/OpenCAEPoro/${CompileMode}/data
cp ${ROOT_PATH}/OpenCAEPoro/lib/libOpenCAEPoroX.a ${ROOT_PATH}/softwares/OpenCAEPoro/${CompileMode}/lib/
cp ${ROOT_PATH}/OpenCAEPoro/testOpenCAEPoro ${ROOT_PATH}/softwares/OpenCAEPoro/${CompileMode}/
cp -r ${ROOT_PATH}/OpenCAEPoro/data/* ${ROOT_PATH}/softwares/OpenCAEPoro/${CompileMode}/data